package com.kyzh.core.dialog

import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import com.kyzh.core.R
import com.kyzh.core.current.AppImpl
import com.kyzh.core.current.MainDialogInfo
import com.kyzh.core.utils.loadImage
import com.kyzh.core.utils.startGameDetailActivity
import org.jetbrains.anko.sdk27.coroutines.onClick
import java.text.SimpleDateFormat
import java.util.*

/**
 *
 * @Author:         yu
 * @CreateDate:     2021/8/5 14:02
 */

private var pactDialog: AlertDialog? = null
private var mainDialogInfo : MainDialogInfo?= null
private var showDialog = false
//是否显示过了弹窗

fun Activity.showMainView() {
    val activity = this
    AppImpl.getMainDialog {
        if(this?.status == "1"){
            if(this?.type== "1"){
                mainDialogInfo = this
                if(!showDialog){
                    showDialog=true
                    pactDialog = AlertDialog.Builder(activity, R.style.kyzhGuestLoginDialog1)
                        .setView(getMainDialogView(mainDialogInfo))
                        .setCancelable(false).create()
                    val dialogWindow: Window? = pactDialog!!.window
                    dialogWindow?.setBackgroundDrawableResource(android.R.color.transparent)

                    val lp: WindowManager.LayoutParams? = dialogWindow?.getAttributes()
                    dialogWindow?.setGravity(Gravity.CENTER)
                    lp?.width = resources.displayMetrics.widthPixels // 宽度
                    dialogWindow?.setAttributes(lp)
                    pactDialog?.show()}
            }else{
                if(! showDialog){
                    showDialog=true
                    if(isTodayFirstStartApp(activity)){
                        mainDialogInfo = this
                        pactDialog = AlertDialog.Builder(activity, R.style.kyzhGuestLoginDialog1)
                            .setView(getMainDialogView(mainDialogInfo))
                            .setCancelable(false).create()
                        val dialogWindow: Window? = pactDialog!!.window
                        dialogWindow?.setBackgroundDrawableResource(android.R.color.transparent)
                        val lp: WindowManager.LayoutParams? = dialogWindow?.getAttributes()
                        dialogWindow?.setGravity(Gravity.CENTER)
                        lp?.width = resources.displayMetrics.widthPixels // 宽度
                        dialogWindow?.setAttributes(lp)
                        pactDialog?.show()
                    }}
            }
        }
    }


}

fun isTodayFirstStartApp(context: Context): Boolean {
    try {
        val preferences: SharedPreferences = context.getSharedPreferences("TODAY_FIRST_START_APP", Context.MODE_PRIVATE)
        val svaeTime = preferences.getString("startAppTime", "2020-01-08")
        val todayTime: String = SimpleDateFormat("yyyy-MM-dd").format(Date())
        if (!TextUtils.isEmpty(todayTime) && !TextUtils.isEmpty(svaeTime)) {
            if (svaeTime != todayTime) {
                preferences.edit().putString("startAppTime", todayTime).commit()
                return true
            }
        }
    } catch (e: Exception) {
//        log(TAG, "是否为今日首次启动APP,获取异常：$e")
        return true
    }
    return false
}

private fun Activity.getMainDialogView(mainDialogInfo :MainDialogInfo?): View {
    val view = View.inflate(this, R.layout.dialog_main, null)
    val img = view.findViewById<ImageView>(R.id.img)
    val close = view.findViewById<ImageView>(R.id.close)
    img.loadImage(mainDialogInfo?.image)
    close.setImageResource(R.drawable.main_dialog_close)
    close.onClick {
        pactDialog?.dismiss()
    }
    img.onClick {
        pactDialog?.dismiss()
        if(mainDialogInfo?.gid.isNullOrEmpty()||mainDialogInfo?.gid=="0"){
            return@onClick
        }
        startGameDetailActivity(mainDialogInfo?.gid)
    }

    return view
}